package com.tracesystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tracesystem.commons.Result;
import com.tracesystem.commons.annotation.RuleValidate;
import com.tracesystem.controller.form.ActivRoleForm;
import com.tracesystem.controller.form.BanRoleForm;
import com.tracesystem.controller.form.ChangeRoleInfoForm;
import com.tracesystem.controller.form.RoleForm;
import com.tracesystem.model.IssueRole;
import com.tracesystem.service.IssueRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Kevin Martin
 * @since 2022-03-23
 */
@RestController
@RequestMapping("/issueRole")
@Api(tags = "角色操作类")
public class IssueRoleController {
    @Resource
    private IssueRoleService issueRoleService;

    /**
     *添加角色
     * @param roleForm 参数对象接受添加角色的名称，添加角色的人
     * @return
     */
    @RuleValidate
    @ApiOperation("添加角色")
    @PostMapping("newRole")
    public Result<?> newRole(@RequestBody @Validated RoleForm roleForm) {
        return issueRoleService.addRole(roleForm);

    }


    /**
     * 查询可以用角色
     *
     * @return
     */
    @RuleValidate
    @ApiOperation("查询所有可用角色")
    @GetMapping("getRoleList")
    public Result<List<IssueRole>> getRoleList() {
        QueryWrapper<IssueRole> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("MODULE_STATE", "启用");
        return Result.getInstanceList(IssueRole.class).setData(issueRoleService.list(queryWrapper));
    }

    /**
     * 查询所有角色包括禁用的角色
     *
     * @return
     */

    @RuleValidate
    @ApiOperation("查询所有角色包括禁用的角色")
    @GetMapping("getRoleListwithclose")
    public Result<List<IssueRole>> getRoleListwithclose() {
        return Result.getInstanceList(IssueRole.class).setData(issueRoleService.list(null));
    }

    /**
     * 修改角色信息
     *
     * @param changeRoleInfoForm
     * @return
     */
    @RuleValidate
    @ApiOperation("修改角色信息")
    @PostMapping("changeroleinfo")
    public Result<?> changeRole(@RequestBody @Validated ChangeRoleInfoForm changeRoleInfoForm) {
        return issueRoleService.changeRoleinfo(changeRoleInfoForm);
    }

    /**
     * 禁用角色
     *
     * @return
     */
    @RuleValidate
    @ApiOperation("禁止角色")
    @PostMapping("banRole")
    public Result<?> banRole(@RequestBody @Validated BanRoleForm banRoleForm) {
        return issueRoleService.stopRole(banRoleForm);
    }

    /**
     * 激活角色
     *
     * @return
     */
    @RuleValidate
    @ApiOperation("激活角色")
    @PostMapping("activeRole")
    public Result<?> activeRole(@RequestBody @Validated ActivRoleForm activRoleForm) {
        return issueRoleService.activeRole(activRoleForm);
    }


}
